<?php

namespace AuctionCore\CarDealer;

use AuctionCore\System\ListBase;
use AuctionCore\AntNest\UserCenter;
use AuctionCore\CarDealer\Dealer;

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

/**
 * "车商保证金"列表
 * @author       爱民
 */
class BailList
{

    public function __construct()
    {

    }

    public function get($page, $limit, $params = array())
    {
        $page = intval($page) < 1 ? 1 : $page;
        $limit = intval($limit) < 1 ? 10 : $limit;
        $map = array();
        $map['level'] = ['gt', 0];
        $map['is_agree_agreement'] = 1;
        //车商ID
        if (!empty($params['dealer_id'])) {
            $map['cd.dealer_id'] = intval($params['dealer_id']);
        }
        //车商联系人
        if (!empty($params['contact_person'])) {
            $map['cd.contact_person'] = ['like', '%' . trim($params['contact_person']) . '%'];
        }
        //联系人电话
        if (!empty($params['contact_mobile'])) {
            $uc = new UserCenter();
            $info1 = $uc->info_bymobile(trim($params['contact_mobile']), 2, 1);
            $map['du.uid'] = $info1['account_id'];
            //$map['contact_mobile'] = trim($params['contact_mobile']);
        }
        $cols = "cd.dealer_id,cd.contact_person,du.uid,cd.bail_amount,cd.freeze_amount,
            cd.indemnity_amount,cd.minus_amount";
        $prefix = C('DB_PREFIX');
        $count = M("car_dealer")->alias('cd')
            ->join($prefix . "dealer_user as du ON cd.dealer_id=du.dealer_id")
            ->where($map)->count();
        $list = M("car_dealer")->alias('cd')->field($cols)
            ->join($prefix . "dealer_user as du ON cd.dealer_id=du.dealer_id")
            ->where($map)->page($page)->limit($limit)->order("dealer_id desc")->select();
        if (!empty($list)) {
            $dealer = new Dealer();
            $list = $dealer->get_contact_mobile_byAntNest($list);
        }
        $data = array(
            'count' => $count,
            'list' => $list
        );
        return $data;
    }


    public function get_offline_logs($page = 1, $limit = 10, $params = array())
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $limit = (int)$limit < 1 ? 10 : (int)$limit;
        $map = array();
        $map['recharge_method'] = array('eq', 3);
        $map['use_type'] = array('eq', 1);
        //车商ID
        if (!empty($params['dealer_id'])) {
            $map['d.dealer_id'] = array('like', $params['dealer_id']);
        }
        //联系人
        if (!empty($params['real_name'])) {
            $map['d.contact_person'] = array('like', $params['real_name']);
        }
        //联系电话
        if (!empty($params['mobile'])) {
            $uc = new UserCenter();
            $info1 = $uc->info_bymobile($params['mobile'], 2, 1);
            $map['du.uid'] = array('eq', $info1['account_id']);
            // $map['d.contact_mobile'] = array('eq', $params['mobile']);
        }
        //充值时间
        if (!empty($params['start_time']) || !empty($params['end_time'])) {
            if (empty($params['start_time'])) {
                $map['dbl.createtime'] = array("elt", $params['end_time']);
            } elseif (empty($params['end_time'])) {
                $map['dbl.createtime'] = array("gt", $params['start_time']);
            } else {
                $map['dbl.createtime'] = array(array("gt", $params['start_time']), array("elt", $params['end_time']));
            }
        }

        $prefix = C('DB_PREFIX');
        //$cols = 'du.dealer_id,d.contact_person,d.contact_mobile,dp.name branch_name,d.depart_id,dbl.occur_money,dbl.bl_id,dbl.createtime';
        $cols = 'd.dealer_id,d.contact_person,du.uid,d.depart_id,dbl.occur_money,dbl.bl_id,dbl.createtime';
        $total = M('dealer_bail_log')->alias('dbl')
            ->join($prefix . 'car_dealer as d ON dbl.dealer_id=d.dealer_id')
            ->join($prefix . "dealer_user as du ON d.dealer_id=du.dealer_id")
            ->where($map)->count();

        $list = M('dealer_bail_log')->alias('dbl')->field($cols)
            ->join($prefix . 'car_dealer as d ON dbl.dealer_id=d.dealer_id')
            ->join($prefix . "dealer_user as du ON d.dealer_id=du.dealer_id")
            ->where($map)->page($page)->limit($limit)->order('dbl.createtime desc')->select();
        if (!empty($list)) {
            $dealer = new Dealer();
            $list = $dealer->get_contact_mobile_byAntNest($list);
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * @param int $page
     * @param int $limit
     * @param array $params
     * @return array
     */
    public function get_lakala_logs($page = 1, $limit = 10, $params = array())
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $limit = (int)$limit < 1 ? 10 : (int)$limit;
        $map = array();
        //充值方式
        if ($params['recharge_method'] > 0) {
            $map['dbl.recharge_method'] = array('eq', $params['recharge_method']);
        }
        //车商分公司
        if ($params['bc_id'] > -1) {
            $map['rl.branch_id'] = $params['bc_id'];
        }
        //车商ID
        if (!empty($params['dealer_id'])) {
            $map['d.dealer_id'] = $params['dealer_id'];
        }
        //联系人
        if (!empty($params['real_name'])) {
            $map['d.contact_person'] = array('like', '%' . $params['real_name'] . '%');
        }
        //联系电话
        if (!empty($params['mobile'])) {
            $uc = new UserCenter();
            $info1 = $uc->info_bymobile($params['mobile'], 2, 1);
            $map['du.uid'] = array('eq', $info1['account_id']);
            //$map['d.contact_mobile'] = $params['mobile'];
        }
        //订单号
        if (!empty($params['order_no'])) {
            $map['dbl.recharge_no'] = $params['order_no'];
        }
        //交易号
        if (!empty($params['transaction_id'])) {
            $map['rl.transaction_id'] = $params['transaction_id'];
        }
        //充值时间
        if (!empty($params['start_time']) || !empty($params['end_time'])) {
            if (empty($params['start_time'])) {
                $map['rl.deal_time'] = array("elt", $params['end_time']);
            } elseif (empty($params['end_time'])) {
                $map['rl.deal_time'] = array("gt", $params['start_time']);
            } else {
                $map['rl.deal_time'] = array(array("gt", $params['start_time']), array("elt", $params['end_time']));
            }
        }

        $prefix = C('DB_PREFIX');
        $cols = 'd.dealer_id,d.contact_person,du.uid,dp.name branch_name,d.depart_id,dbl.occur_money,rl.branch_id,rl.order_no,rl.order_fee,rl.transaction_id,rl.deal_time';
        $total = M('dealer_bail_log')->alias('dbl')
            ->join($prefix . 'car_dealer as d ON dbl.dealer_id=d.dealer_id')
            ->join($prefix . "dealer_user as du ON d.dealer_id=du.dealer_id")
            ->join($prefix . 'recharge_log as rl ON rl.id=dbl.recharge_id')
            ->join("LEFT JOIN " . $prefix . 'depart as dp ON rl.branch_id=dp.id')
            ->where($map)->count();

        $list = M('dealer_bail_log')->alias('dbl')->field($cols)
            ->join($prefix . 'car_dealer as d ON dbl.dealer_id=d.dealer_id')
            ->join($prefix . "dealer_user as du ON d.dealer_id=du.dealer_id")
            ->join($prefix . 'recharge_log as rl ON rl.id=dbl.recharge_id')
            ->join("LEFT JOIN " . $prefix . 'depart as dp ON rl.branch_id=dp.id')
            ->where($map)->page($page)->limit($limit)->order('dbl.createtime desc')->select();
        //echo M()->getLastSql();
        if (!empty($list)) {
            $dealer = new Dealer();
            $list = $dealer->get_contact_mobile_byAntNest($list);
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;

    }
}
//end
